package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Optional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SectorDetail;
import com.ruoyi.system.domain.Stocklist;
import com.ruoyi.system.service.ISectorDetailService;
import com.ruoyi.system.util.PathUtils;
import com.ruoyi.system.util.pythonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SectorSpotMapper;
import com.ruoyi.system.domain.SectorSpot;
import com.ruoyi.system.service.ISectorSpotService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 板块行情Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-27
 */
@Service
public class SectorSpotServiceImpl  implements ISectorSpotService
{
    @Autowired
    private SectorSpotMapper sectorSpotMapper;

    @Autowired
    private ISectorDetailService sectorDetailService;

    /**
     * 查询板块行情
     * 
     * @param id 板块行情主键
     * @return 板块行情
     */
    @Override
    public SectorSpot selectSectorSpotById(Long id)
    {
        return sectorSpotMapper.selectSectorSpotById(id);
    }
    @Override
    public SectorSpot getOne(SectorSpot stocklist) {
        return Optional.ofNullable(this.selectSectorSpotList(stocklist)).map(stocklists ->{
            if (stocklists.size() > 0) {
                return stocklists.get(0);
            }
            return null;
        }).orElse(null);

    }
    /**
     * 查询板块行情列表
     * 
     * @param sectorSpot 板块行情
     * @return 板块行情
     */
    @Override
    public List<SectorSpot> selectSectorSpotList(SectorSpot sectorSpot)
    {
        return sectorSpotMapper.selectSectorSpotList(sectorSpot);
    }

    /**
     * 新增板块行情
     * 
     * @param sectorSpot 板块行情
     * @return 结果
     */
    @Override
    public int insertSectorSpot(SectorSpot sectorSpot)
    {
        return sectorSpotMapper.insertSectorSpot(sectorSpot);
    }

    /**
     * 修改板块行情
     * 
     * @param sectorSpot 板块行情
     * @return 结果
     */
    @Override
    public int updateSectorSpot(SectorSpot sectorSpot)
    {
        return sectorSpotMapper.updateSectorSpot(sectorSpot);
    }

    /**
     * 批量删除板块行情
     * 
     * @param ids 需要删除的板块行情主键
     * @return 结果
     */
    @Override
    public int deleteSectorSpotByIds(String ids)
    {
        return sectorSpotMapper.deleteSectorSpotByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除板块行情信息
     * 
     * @param id 板块行情主键
     * @return 结果
     */
    @Override
    public int deleteSectorSpotById(Long id)
    {
        return sectorSpotMapper.deleteSectorSpotById(id);
    }


    @Transactional
    public void getSectorSpot() {
        String[] classifys = {"新浪行业", "概念", "地域", "行业"};
        for (String classify : classifys) {
            String[] url = new String[]{"python", PathUtils.PATH + "stock_sector_spot.py", classify};
            List<String> stocks = pythonUtil.executeCMD(url);
            for (String sector : stocks) {
                String[] split = sector.split(",");
                SectorSpot sectorSpotEntity = new SectorSpot(split[0], split[1], split[2], classify);
                SectorDetail detailOld =new SectorDetail();
                detailOld.setLabel(split[0]);
                detailOld.setDate(DateUtils.parseDate(DateUtils.dateTime()));
                detailOld = sectorDetailService.getOne(detailOld);
                SectorDetail detailNew = new SectorDetail(split[0], DateUtils.getNowDate(), split[3], split[4], split[5], split[6], split[7]);
                Optional.ofNullable(detailOld).map(old -> {
                    detailNew.setId(old.getId());
                    return sectorDetailService.updateSectorDetail(detailNew);
                }).orElse(sectorDetailService.insertSectorDetail(detailNew));

                SectorSpot spotOld =new SectorSpot();
                spotOld.setLabel(sectorSpotEntity.getLabel());
                spotOld = this.getOne(spotOld);
                if (spotOld != null) {
                    if (spotOld.getSymbols() !=null && Integer.parseInt(sectorSpotEntity.getNum()) != spotOld.getSymbols().split(",").length) {
                        sectorSpotEntity.setSymbols(getSectorSpotDetail(sectorSpotEntity.getLabel()));
                    }
                    sectorSpotEntity.setId(spotOld.getId());
                    this.updateSectorSpot(sectorSpotEntity);
                } else {
                    sectorSpotEntity.setSymbols(getSectorSpotDetail(sectorSpotEntity.getLabel()));
                    this.insertSectorSpot(sectorSpotEntity);
                }
            }
        }

    }

    private String getSectorSpotDetail(String lebel) {
        String[] url = new String[]{"python", PathUtils.PATH + "stock_sector_detail.py", lebel};
        List<String> stocks = pythonUtil.executeCMD(url);
        StringBuffer codes = new StringBuffer();
        for (String detail : stocks) {
            codes.append(detail.split(",")[0] + ",");
        }
        if (StringUtils.isBlank(codes.toString())) {
            return null;
        }
        return codes.toString().substring(0, codes.length() - 1);
    }
}
